﻿using System;

namespace Yaml.Presentation.Parsing.TokenScanners {
    class BlockEntryTokenScanner : BasicTokenScanner {

        public BlockEntryTokenScanner(Scanner scanner)
            : base(scanner, '-') {
        }

        protected override bool CheckForToken() {
            return base.CheckForToken() && Scanner.IsSpaceLineBreakOrNullChar(Reader.PeekRelative(1));
        }

        protected override void BeforeScan() {
            base.BeforeScan();
            if (Scanner.State.NotInFlow) {
                if (!Scanner.State.SimpleKeys.IsSimpleKeyAllowed) {
                    throw new ScannerException("Sequence entries are not allowed here, " + Start.ToString());
                }
                if (Scanner.State.Indentation.Push(Scanner.State.Column)) {
                    Mark m = Scanner.GetMark();
                    Scanner.Tokens.Push(new Tokens.BlockSequenceStartToken(Start, m, Scanner.State.Indentation.Current));
                }
            }
        }

        protected override void AfterScan() {
            base.AfterScan();
            // Reset possible simple key in this flow level
            RemoveSimpleKey(true);
        }

        protected override Yaml.Presentation.Tokens.Token CreateToken() {
            return new Tokens.BlockEntryToken(Start, End, Scanner.State.Indentation.Current);
        }
    }
}
